package CRUD;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

public class ReadWriteDemo {
	public static void main(String[] args) {
		try {
			long begin  = System.currentTimeMillis();
			String path = "C:\\b.plmxml";//测试文件在a.plmxml/src/source目录下
			read(path);
			long end  = System.currentTimeMillis();
			System.out.println("dom4j用时"+(end-begin)+"毫秒");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void read(String plmxml) {
		try {
			SAXReader reader = new SAXReader();
			Document document = reader.read(new File(plmxml));// 读取xml
			Element root = document.getRootElement();// 获取xml根节点
			Element InstanceGraph = root.element("ProductDef").element(
					"InstanceGraph");// 获取/PLMXML/ProductDef/InstanceGraph节点
			List nodes = InstanceGraph.elements("ProductRevisionView");// 获取InstanceGraph节点下的所有ProductRevisionView节点
			for (Iterator it = nodes.iterator(); it.hasNext();) {// 遍历ProductRevisionView节点
				Element elm = (Element) it.next();
				String tempItemID = elm.attributeValue("name");// 取得ProductRevisionView节点的name属性的值
				if (!tempItemID.endsWith("(view)")) {// 根据name属性的值判断是否是JT单件

					int index = tempItemID.indexOf("-", 4);// 取得JT单件号第中杠线的位置（例如，J43-6302010/06-行李箱盖护罩）
					String temp = tempItemID.substring(0, index);
					String itemID = temp.substring(0, temp.indexOf("/"));// 获取零件号
					String ietmVesion = temp.substring(temp.indexOf("/") + 1,
							index);// 获取版本号
					//
					Element Representation = elm.element("Representation");// 获取ProductRevisionView节点下的Representation节点
					String jt = Representation.attributeValue("format");// 获取Representation节点下的format属性值
					String location = Representation.attributeValue("location");// 获取Representation节点下的location属性值
					System.out.println("itemID=" + itemID + "    ietmVesion="
							+ ietmVesion + "    jt=" + jt + "   location="
							+ location);
					String jtLocationName = itemID + "_" + ietmVesion + ".jt";// 新的jt文件名，命名规则为：零件号+"_"+版本号.jt
					List tempList = Representation.attributes();
					Attribute RepresentationLocation = Representation
							.attribute("location");// 获取location属性对象
					RepresentationLocation.setValue(jtLocationName);// 更改location属性值
					Representation.setAttributes(tempList);// 更改location属性值
				}
			}
			try {
				OutputFormat format = OutputFormat.createPrettyPrint();// 把内存更改后的值写入本地文件
				format.setEncoding("UTF-8");
				XMLWriter writer = new XMLWriter(new FileOutputStream(
						new File(plmxml)), format);
				writer.write(document);
				writer.close();
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		} catch (DocumentException e) {
			e.printStackTrace();
		}
	}
}
